home *** CD-ROM | disk | FTP | other *** search
- /* ========================================
-
- ls.c
-
- Copyright (c) 1993,1994 Newport Software Development
-
- You may distribute unmodified copies of this file for
- noncommercial purposes. You may use this file as a
- reference when writing your own nShell(tm) commands.
-
- All other rights are reserved.
-
- ======================================== */
-
- #include <Packages.h>
-
- #define LS_BUF_SIZ 4000
-
- #include "nshc.h"
-
- #include "arg_utl.proto.h"
- #include "str_utl.proto.h"
- #include "nshc_utl.proto.h"
-
- /* ======================================== */
-
- void format_size( long size, char *p );
-
- void format_size( long size, char *p )
- {
- char units;
-
- units = ' ';
-
- if (size > 9999) {
- size /= 1024;
- units = 'k';
- }
-
- if (size > 9999) {
- size /= 1024;
- units = 'M';
- }
-
- cStrFromNum( p, 4, size );
-
- p += 4;
-
- *p++ = units;
- }
-
- /* ======================================== */
-
- short ls_volume( Str255 path );
-
- short ls_volume( Str255 path )
- {
- Str255 localPath;
- HParamBlockRec pb;
-
- pb.volumeParam.ioCompletion = 0;
- pb.volumeParam.ioNamePtr = localPath;
- pb.volumeParam.ioVRefNum = 0;
- pb.volumeParam.ioVolIndex = -1;
- pStrCopy( localPath, path );
-
- if (!PBHGetVInfo(&pb, FALSE))
- return( pb.volumeParam.ioVRefNum );
- else
- return(0);
- }
-
- /* ======================================== */
-
- void long_header( t_nshc_calls *nshc_calls, int option_time );
-
- void long_header( t_nshc_calls *nshc_calls, int option_time )
- {
- switch( option_time ) {
- case 'g' :
- nshc_calls->NSH_putStr("\p\r Crea Type RSize DSize Creation Date Name");
- break;
- case 'b' :
- nshc_calls->NSH_putStr("\p\r Crea Type RSize DSize Backup Date Name");
- break;
- default :
- nshc_calls->NSH_putStr("\p\r Crea Type RSize DSize Modification Date Name");
- break;
- }
- nshc_calls->NSH_putStr("\p\r ---- ---- ----- ----- -------------------- ----\r");
- }
-
- /* ======================================== */
-
- int show_long( t_nshc_calls *nshc_calls, HFileInfo *fpb, Str255 path, int option_time );
-
- int show_long( t_nshc_calls *nshc_calls, HFileInfo *fpb, Str255 path, int option_time )
- {
- int i;
- int index;
- OSType type;
- char *p;
- char line[LS_BUF_SIZ];
- long value;
- Str255 string;
-
- index = 0;
- line[index++] = ' ';
-
- if (fpb->ioFlAttrib & 16) {
-
- for (i=1; i<23; i++) line[index++] = ' ';
-
- switch( option_time ) {
- case 'g' :
- value = ((DirInfo *)fpb)->ioDrCrDat;
- break;
- case 'b' :
- value = ((DirInfo *)fpb)->ioDrBkDat;
- break;
- default :
- value = ((DirInfo *)fpb)->ioDrMdDat;
- break;
- }
-
- IUDateString( value, shortDate, string );
- for (i=1; i<=(8 - string[0]); i++) line[index++] = ' ';
- for (i=1; i<=string[0]; i++) line[index++] = string[i];
- line[index++] = ' ';
-
- IUTimeString( value, 1, string );
- for (i=1; i<=(11 - string[0]); i++) line[index++] = ' ';
- for (i=1; i<=string[0]; i++) line[index++] = string[i];
- line[index++] = ' ';
-
- }
- else {
-
- type = fpb->ioFlFndrInfo.fdCreator;
- p = (char *) &type;
- for ( i=0; i<4; i++ ) line[index++] = *p++;
- line[index++] = ' ';
-
- type = fpb->ioFlFndrInfo.fdType;
- p = (char *) &type;
- for ( i=0; i<4; i++ ) line[index++] = *p++;
- line[index++] = ' ';
-
- format_size( (fpb->ioFlRLgLen), &line[index] );
- index += 5;
- line[index++] = ' ';
-
- format_size( (fpb->ioFlLgLen), &line[index] );
- index += 5;
- line[index++] = ' ';
-
- switch( option_time ) {
- case 'g' :
- value = fpb->ioFlCrDat;
- break;
- case 'b' :
- value = fpb->ioFlBkDat;
- break;
- default :
- value = fpb->ioFlMdDat;
- break;
- }
-
- IUDateString( value, shortDate, string );
- for (i=1; i<=(8 - string[0]); i++) line[index++] = ' ';
- for (i=1; i<=string[0]; i++) line[index++] = string[i];
- line[index++] = ' ';
-
- IUTimeString( value, 1, string );
- for (i=1; i<=(11 - string[0]); i++) line[index++] = ' ';
- for (i=1; i<=string[0]; i++) line[index++] = string[i];
- line[index++] = ' ';
- }
-
- for ( i=1; i<=path[0]; i++ )
- line[index++] = path[i];
-
- line[index++] = '\r';
- line[index] = 0;
-
- nshc_calls->NSH_puts(line);
- }
-
- /* ======================================== */
-
- int do_long( t_nshc_calls *nshc_calls, short volume, Str255 path, int option_time, int is_list, int first_call );
-
- int do_long( t_nshc_calls *nshc_calls, short volume, Str255 path, int option_time, int is_list, int first_call )
- {
- CInfoPBRec cipbr;
- HFileInfo *fpb;
- short idx;
- short foundDir;
- Str255 localPath;
-
- fpb = (HFileInfo *)&cipbr;
-
- fpb->ioVRefNum = volume;
- fpb->ioDirID = 0;
- fpb->ioFDirIndex = 0;
- fpb->ioNamePtr = localPath;
- pStrCopy( localPath, path );
-
- if (PBGetCatInfo( &cipbr, FALSE )) {
- nshc_calls->NSH_putStr_err("\pls: ioDirID not found.\r");
- return(1);
- }
-
- foundDir = fpb->ioDirID;
-
- if (fpb->ioFlAttrib & 16) {
- if (is_list) {
- nshc_calls->NSH_putStr("\p\rDir = ");
- nshc_calls->NSH_putStr(path);
- nshc_calls->NSH_putStr("\p\r\r");
- }
- if (is_list || first_call) {
- long_header( nshc_calls, option_time );
- }
- for( idx=1; TRUE; idx++) { /* indexing loop */
- fpb->ioVRefNum = volume;
- fpb->ioDirID = foundDir; /* must set on each loop */
- fpb->ioFDirIndex = idx;
- pStrCopy( localPath, path );
-
- if (PBGetCatInfo( &cipbr, FALSE )) break; /* exit when no more entries */
-
- show_long( nshc_calls, fpb, localPath, option_time );
- }
- nshc_calls->NSH_putchar('\r');
- }
- else
- show_long( nshc_calls, fpb, localPath, option_time );
-
- return(0);
- }
-
- /* ======================================== */
-
- int do_cols( t_nshc_calls *nshc_calls, short volume, Str255 path, int cols, int is_list );
-
- int do_cols( t_nshc_calls *nshc_calls, short volume, Str255 path, int cols, int is_list )
- {
- CInfoPBRec cipbr;
- HFileInfo *fpb;
- short idx, foundDir;
- Str255 localPath;
- int blanks;
- int count;
- int column_width;
- int i, cmax;
- int buf_ptr;
- char buf[LS_BUF_SIZ];
-
- fpb = (HFileInfo *)&cipbr;
-
- fpb->ioVRefNum = volume;
- fpb->ioDirID = 0;
- fpb->ioFDirIndex = 0;
- fpb->ioNamePtr = localPath;
- pStrCopy( localPath, path );
-
- if (PBGetCatInfo( &cipbr, FALSE )) {
- nshc_calls->NSH_putStr_err("\pls: ioDirID not found.\r");
- return(1);
- }
-
- if (fpb->ioFlAttrib & 16) {
-
- if (is_list) {
- nshc_calls->NSH_putStr("\p\rDir = ");
- nshc_calls->NSH_putStr(path);
- nshc_calls->NSH_putStr("\p\r\r");
- }
-
- foundDir = fpb->ioDirID;
- count = 0;
-
- column_width = 0;
-
- for( idx=1; TRUE; idx++) {
- fpb->ioVRefNum = volume;
- fpb->ioDirID = foundDir;
- fpb->ioFDirIndex = idx;
- pStrCopy( localPath, path );
- if (PBGetCatInfo( &cipbr, FALSE )) // returns non-zero when no more
- break;
- i = localPath[0] + 5;
- if (i > column_width) column_width = i;
- }
-
- buf_ptr = 0;
-
- for( idx=1; TRUE; idx++) {
- fpb->ioVRefNum = volume;
- fpb->ioDirID = foundDir;
- fpb->ioFDirIndex = idx;
- pStrCopy( localPath, path );
-
- if (PBGetCatInfo( &cipbr, FALSE )) // returns non-zero when no more
- break;
-
- count++;
-
- cmax = localPath[0];
-
- if ( (buf_ptr + column_width + 2 ) > LS_BUF_SIZ ) {
- buf[buf_ptr] = 0;
- nshc_calls->NSH_puts(buf);
- buf_ptr = 0;
- }
-
- for ( i = 1 ; i <= cmax ; i ++ )
- buf[buf_ptr++] = localPath[i];
-
- blanks = column_width - cmax;
-
- if (fpb->ioFlAttrib & 16) {
- buf[buf_ptr++] = ':';
- blanks--;
- }
-
- if (count % cols) {
- while (blanks--)
- buf[buf_ptr++] = ' ';
- }
- else {
- buf[buf_ptr++] = '\r';
- count = 0;
- }
- }
-
- if (buf_ptr) {
- buf[buf_ptr++] = '\r';
- buf[buf_ptr] = 0;
- nshc_calls->NSH_puts(buf);
- }
- nshc_calls->NSH_putchar('\r');
- }
- else {
- nshc_calls->NSH_putStr(localPath);
- nshc_calls->NSH_putchar('\r');
- }
-
-
- return(0);
- }
-
- /* ======================================== */
-
- int do_fast( t_nshc_calls *nshc_calls, short volume, Str255 path, int is_list );
-
- int do_fast( t_nshc_calls *nshc_calls, short volume, Str255 path, int is_list )
- {
- CInfoPBRec cipbr;
- HFileInfo *fpb;
- short idx, foundDir;
- Str255 localPath;
- int ci, cmax;
- int buf_ptr;
- char buf[LS_BUF_SIZ];
-
- fpb = (HFileInfo *)&cipbr;
-
- fpb->ioVRefNum = volume;
- fpb->ioDirID = 0;
- fpb->ioFDirIndex = 0;
- fpb->ioNamePtr = localPath;
- pStrCopy( localPath, path );
-
- if (PBGetCatInfo( &cipbr, FALSE )) {
- nshc_calls->NSH_putStr_err("\pls: File or directory not found.\r");
- return(1);
- }
-
- if (fpb->ioFlAttrib & 16) {
-
- if (is_list) {
- nshc_calls->NSH_putStr("\p\rDir = ");
- nshc_calls->NSH_putStr(path);
- nshc_calls->NSH_putStr("\p\r\r");
- }
-
- foundDir = fpb->ioDirID;
- buf_ptr = 0;
-
- for( idx=1; TRUE; idx++) { /* indexing loop */
- fpb->ioVRefNum = volume;
- fpb->ioDirID = foundDir; /* must set on each loop */
- fpb->ioFDirIndex = idx;
- pStrCopy( localPath, path );
-
- if (PBGetCatInfo( &cipbr, FALSE )) break; /* exit when no more entries */
-
- cmax = localPath[0];
-
- if ( (buf_ptr + cmax + 3 ) > LS_BUF_SIZ ) {
- buf[buf_ptr] = 0;
- nshc_calls->NSH_puts(buf);
- buf_ptr = 0;
- }
- else {
- for ( ci = 1 ; ci <= cmax ; ci ++ )
- buf[buf_ptr++] = localPath[ci];
-
- if (fpb->ioFlAttrib & 16)
- buf[buf_ptr++] = ':';
-
- buf[buf_ptr++] = '\r';
- }
- }
-
- if (buf_ptr) {
- buf[buf_ptr] = 0;
- nshc_calls->NSH_puts(buf);
- }
- nshc_calls->NSH_putchar('\r');
- }
- else {
- nshc_calls->NSH_putStr(localPath);
- nshc_calls->NSH_putchar('\r');
- }
-
- return(0);
- }
-
- /* ======================================== */
-
- void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
- {
- int option_cols;
- int option_long;
- int option_time;
- int cArg;
- int arg;
- int argc;
- int cols;
- Str255 path;
- int path_ok;
- int error;
- int count;
- int first_call;
- short volume;
-
- error = nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION );
-
- if (!error) {
- option_long = nshc_got_option( nshc_parms, 'l' );
- option_cols = nshc_got_option( nshc_parms, 'c' );
- option_time = 'm'; // modification
- if (nshc_got_option( nshc_parms, 'b' )) option_time = 'b';
- if (nshc_got_option( nshc_parms, 'g' )) option_time = 'g';
- if (cArg = nshc_got_option( nshc_parms, 'c' ))
- if ( nshc_is_numeric_operand( nshc_parms, ++cArg ) )
- cols = arg_to_int( nshc_parms, cArg );
- else {
- nshc_calls->NSH_putStr_err("\pUsage: ls [<path1> <path2>...] [-c columns] [-l [-m] [-b] [-g]].\r");
- error = NSHC_ERR_PARMS;
- }
- }
-
- count = 0;
-
- first_call = 1;
-
- argc = nshc_parms->argc;
-
- for (arg = 1; arg < argc; arg++ )
- if (!error)
- if (arg != cArg)
- if (nshc_is_operand(nshc_parms, arg)) {
- count++;
- pStrFromC( path, &(nshc_parms->arg_buf[nshc_parms->argv[arg]]) );
- error = nshc_calls->NSH_path_expand(path);
- if (!error)
- volume = ls_volume( path );
- if (!error && volume) {
- if (option_long)
- error = do_long(nshc_calls, volume, path, option_time, 1, first_call);
- else
- if ((option_cols) && (cols > 1))
- error = do_cols(nshc_calls, volume, path, cols, 1);
- else
- error = do_fast(nshc_calls, volume, path, 1);
- }
- else
- nshc_calls->NSH_putStr_err("\pls: File or directory not found.\r");
- first_call = 0;
- }
-
- if (!(error || count))
- if (nshc_calls->NSH_var_env( "\pPWD", path )) {
- nshc_calls->NSH_putStr_err("\pls: PWD not found.\r");
- error = 1;
- }
- else
- if ( volume = ls_volume( path ) ) {
- if (option_long)
- error = do_long(nshc_calls, volume, path, option_time, 0, 1);
- else
- if (option_cols)
- error = do_cols(nshc_calls, volume, path, cols, 0);
- else
- error = do_fast(nshc_calls, volume, path, 0);
- }
- else {
- nshc_calls->NSH_putStr_err("\pls: PWD volume not found.\r");
- error = 1;
- }
-
- nshc_parms->action = nsh_idle;
- nshc_parms->result = error;
- }
-